<template>
<div class="user-container">
    <el-card class="user-card" shadow="never">
        <!-- 搜索区域 -->
        <div class="search-bar">
            <el-form :inline="true" :model="search" class="search-form">
                <el-form-item label="用户名">
                    <el-input v-model="search.username" placeholder="请输入用户名" clearable />
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="search.phone" placeholder="请输入手机号" clearable />
                </el-form-item>
                <el-form-item label="状态">
                    <el-select style="width: 150px" v-model="search.status" placeholder="请选择状态" clearable>
                        <el-option label="启用" :value="1" />
                        <el-option label="禁用" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="pageSearch()">
                        <el-icon>
                            <Search />
                        </el-icon>搜索
                    </el-button>
                    <el-button @click="search = getInitUserSearch()">
                        <el-icon>
                            <Refresh />
                        </el-icon>重置
                    </el-button>
                </el-form-item>
            </el-form>
        </div>

        <!-- 用户表格 -->
        <el-table :data="userList" border stripe style="width: 100%" height="calc(100vh - 280px)">
            <el-table-column type="index" label="序号" width="60" align="center" />
            <el-table-column label="用户信息" min-width="200">
                <template #default="{ row }">
                    <div class="user-info">
                        <el-avatar :size="40" :src="row.image" />
                        <div class="user-detail">
                            <div class="user-name">{{ row.username }}</div>
                            <div class="user-phone">{{ row.phone }}</div>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="下单数" width="100" align="center">
                <template #default="{ row }">
                    <el-tag type="success" effect="plain">{{ row.orderCount }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="消费总额" width="120" align="right">
                <template #default="{ row }">
                    <span class="amount">￥{{ row.totalConsumption }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="注册时间" width="180" align="center" />
            <el-table-column prop="lastLoginTime" label="最后登录" width="180" align="center" />
            <el-table-column prop="status" label="状态" width="100" align="center">
                <template #default="{ row }">
                    <el-switch 
                        inline-prompt 
                        v-model="row.status" 
                        active-text="启用" 
                        inactive-text="禁用" 
                        @change="handleStatusChange(row)" 
                    />
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination">
            <el-pagination 
                v-model:current-page="page.currentPage" 
                v-model:page-size="page.pageSize" 
                :page-sizes="[5, 10, 20]"
                :total="page.total" 
                layout="total, sizes, prev, pager, next, jumper" 
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </el-card>
</div>
</template>

<script setup lang="ts">
import { Page } from '@/models/baseModel';
import { getInitUserSearch, User, UserApi, UserSearch } from '@/models/userModel';
import { onMounted, ref } from 'vue';

// 搜索条件
const search = ref<UserSearch>(getInitUserSearch())
// 分页实体
const page = ref<Page>({currentPage: 1, pageSize: 5, total: 0})
// 用户数据
const userList = ref<User[]>([])

onMounted(() => {
    // 加载用户数据
    pageSearch()
})

// 修改用户状态
const handleStatusChange = (row: User) => {
    UserApi.setStatus(row.id, row.status)
}

// 分页获取数据
const pageSearch = (currentPage: undefined | number = undefined) => {
    if (currentPage) page.value.currentPage = currentPage
    UserApi.pagingSearch(search.value, page.value)
    .then(resultPage => {
        page.value.currentPage = resultPage.currentPage
        page.value.pageSize = resultPage.pageSize
        page.value.total = resultPage.total
        userList.value = resultPage.data
    })
}

// 每页大小改变时
const handleSizeChange = (val: number) => {
    page.value.pageSize = val
    pageSearch()
}

// 页码改变时
const handleCurrentChange = (val: number) => {
    page.value.currentPage = val
    pageSearch()
}
</script>

<style scoped>
.user-container {
    height: 100%;
}

.user-card {
    height: 100%;
    border-radius: 8px;
}

.search-bar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 16px;
}

:deep(.el-card__body) {
    height: calc(100% - 40px);
    display: flex;
    flex-direction: column;
    padding: 16px;
}

:deep(.el-table) {
    flex: 1;
}

:deep(.el-table__row) {
    height: 60px;
}

:deep(.el-table__cell) {
    padding: 8px 0;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-detail {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.user-name {
    font-size: 14px;
    color: #303133;
    font-weight: 500;
}

.user-phone {
    font-size: 13px;
    color: #909399;
}

.amount {
    color: #f56c6c;
    font-weight: bold;
}

.operation-group {
    display: flex;
    justify-content: center;
    gap: 12px;
}

.operation-group .el-button {
    font-size: 16px;
    padding: 4px;
}

.pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    padding-right: 20px;
}

/* 表格滚动条样式 */
:deep(.el-table__body-wrapper::-webkit-scrollbar) {
    width: 6px;
    height: 6px;
}

:deep(.el-table__body-wrapper::-webkit-scrollbar-thumb) {
    border-radius: 3px;
    background: #dcdfe6;
}

:deep(.el-table__body-wrapper::-webkit-scrollbar-track) {
    border-radius: 3px;
    background: #f5f7fa;
}

/* 表单样式 */
.user-form {
    padding: 20px;
}

/* 头像上传样式 */
.avatar-uploader {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
}

.avatar {
    width: 100px;
    height: 100px;
    display: block;
    object-fit: cover;
}

/* 对话框样式 */
:deep(.el-dialog__body) {
    padding: 0;
}

.dialog-footer {
    padding: 20px;
    text-align: right;
}

/* 表单项样式 */
:deep(.el-form-item__label) {
    font-weight: 500;
}

:deep(.el-input__wrapper) {
    box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}
</style>